<?php
/**
 * 选择日期
 * Enter description here ...
 * @author nijn
 *
 */
class ChooseTime extends CWidget{
	public $model;
	public $attribute;
	
	/**
	 * 默认的My97DatePicker js配置
	 * <br>如果设置为false或null，该参数不生效
	 * @var mixed
	 */
	public $defaultOptions = array(
		'skin'=>'hayzone',
	);
	
	/**
	 * My97DatePicker 的js配置
	 * @var array
	 */
	public $options = array();
	
	public function run() {
		$this->registerScript();
		
		$htmlOptions = $this->htmlOptions();
		
		
		$name = CActiveFormAdv::resolveName($this->model, $this->attribute).'[]';
		$attrValue = CHtml::value($this->model, $this->attribute);
		
		if (empty($attrValue)) {
			echo CHtml::textField($name,'', array_merge($htmlOptions,array('id'=>'choose-time-input')));
		} else {
			foreach ($attrValue as $one) {
				echo CHtml::textField($name, $one, array_merge($htmlOptions,array('id'=>'choose-time-input')));
			}
		}
		echo CHtml::button('添加日期',array('id'=>'choose-time-button'));
	}
	
	public function htmlOptions() {
		$htmlOptions = array();
		$options = is_array($this->defaultOptions) 
			? array_merge($this->defaultOptions, $this->options)
			: $this->options;
		$jsOptions = CJavaScript::encode($options);
		
		if(isset($htmlOptions['onfocus'])) {
			$htmlOptions['onfocus'] = $htmlOptions['onfocus'].";WdatePicker({$jsOptions})";
		} else {
			$htmlOptions['onfocus'] = "WdatePicker({$jsOptions});";
		}
		return $htmlOptions;
	}
	
	public function registerScript() {
		$cs = Yii::app()->clientScript;
		$cs = Yii::app()->clientScript;
		
		$baseUrl = Yii::app()->assetManager->publish(
			dirname(__FILE__) . '/js/', false, -1, true
		);
		
		$cs->registerScriptFile($baseUrl.'/jquery.chooseTime.js');
	}
}

?>